package com.jy.adscm.operation.cgxsb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cgxsb.bean.CgxsTaskinfo;
import com.jy.adscm.operation.cgxsb.bean.QueryCgxsTaskinfo;
import com.jy.adscm.operation.cgxsb.service.CgxsTaskinfoService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzf
 * @since 2019-01-10
 */
@RestController
            @RequestMapping("adscm/operation/cgxsTaskinfo")
public class CgxsTaskinfoController {
@Autowired
    CgxsTaskinfoService cgxsTaskinfoService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    SimpleDateFormat sd=new SimpleDateFormat("yyyyMMddHHmmssSSS");
    @RequestMapping("/insert")
    public SimpleCoreResponse insert( String data, HttpServletRequest request) {

        CgxsTaskinfo cgxs= null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            cgxs = mapper.readValue(data, CgxsTaskinfo.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        String taskCode=sd.format(new Date()).replaceAll("-","").trim().replaceAll(":","").trim();//设置流水号
        cgxs.setTid(UUID.randomUUID().toString());
        cgxs.setTcode(Long.valueOf(taskCode));
        cgxs.setBzry(String.valueOf(token.getUser().getId()));
//        cgxs.setTssj(new Date());
//        cgxs.setFbsj(new Date());

        return  new SimpleCoreResponse("200","新增成功",cgxsTaskinfoService.insert(cgxs));
}
    @RequestMapping ("/update")
    public SimpleCoreResponse update(String data){
        SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
        CgxsTaskinfo cgxs= null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            cgxs = mapper.readValue(data, CgxsTaskinfo.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (cgxs.getHwzt()==1&&cgxs.getHkzt()==1&&cgxs.getFpzt()==1){
            cgxs.setRwwczt(1);
            cgxs.setWcsj(new Date());
        }
        if (cgxs.getRwlx()==2&&cgxs.getSfhsj()!=null&&cgxs.getSfksj()!=null){
                long day=(cgxs.getSfksj().getTime()-cgxs.getSfhsj().getTime())/(24*60*60*1000);
                cgxs.setDays(String.valueOf(day));
        }
        return new SimpleCoreResponse("200","修改成功",cgxsTaskinfoService.updateById(cgxs));
    }
    @RequestMapping("/selectByid")
    public SimpleCoreResponse selectById(String id){
        return new SimpleCoreResponse("200","查询成功",cgxsTaskinfoService.selectById(id));
    }
    @RequestMapping("/delete")
    public SimpleCoreResponse delete(String id){
        return  new SimpleCoreResponse("200","删除成功",cgxsTaskinfoService.deleteById(id));
    }
    @RequestMapping("/queryByRwlx")
    public SimpleCoreResponse queryByRwlx(String rwlx,String rwzt,String rwwczt,String key){
        System.out.println(rwlx);
        return new  SimpleCoreResponse("200","查询成功",cgxsTaskinfoService.queryByRwlx(rwlx,rwzt,rwwczt,key));
    }
    @RequestMapping("/selectCgxsTaskinfo")
    public SimpleCoreResponse selectCgxsTaskinfo(@RequestBody QueryCgxsTaskinfo queryCgxsTaskinfo){
        Page<CgxsTaskinfo> page=new Page(queryCgxsTaskinfo.getCurrentPage(),queryCgxsTaskinfo.getPageSize());
        page= cgxsTaskinfoService.selectCgxsTaskinfo(page,queryCgxsTaskinfo);

        return new  SimpleCoreResponse("200","查询成功",page);
    }
    @RequestMapping("/queryByKhxx")
    public SimpleCoreResponse queryByKhxx(String type){
        return new SimpleCoreResponse("200","查询成功",cgxsTaskinfoService.queryByKhxx(type));
    }
    @RequestMapping("/queryByYY")
    public  SimpleCoreResponse queryByYY(String roleid){
        return new SimpleCoreResponse("200","查询成功",cgxsTaskinfoService.queryByYY(roleid));
    }



}

